package controller

import (
	"github.com/gofiber/fiber"
	"gowork/planningCalendar/model"
	"log"
	"strconv"
)

type CalendarController struct {
	Model *model.Note
	Message *model.Message
}

func (cc *CalendarController)CreateNote(c *fiber.Ctx){
	cc.Model = new(model.Note)

	n := new(model.Note)
	if bErr := c.BodyParser(n); bErr != nil {
		log.Fatal(bErr)
	}
	res, err := cc.Model.CreateNote(n)
	if err != nil{
		c.JSON(fiber.Map{
			"code": 500,
			"message": err.Error(),
			"data": nil,
		})
	}

	if err := c.JSON(fiber.Map{
		"code": res["code"],
		"message": res["message"],
	}); err != nil {
		c.Status(500).Send(err)
		return
	}
}

func (cc *CalendarController)QueryToDayNote(c *fiber.Ctx){
	cc.Model = new(model.Note)

	adviserIdStr := c.FormValue("adviserId")
	date := c.FormValue("date")
	adviserId, err := strconv.ParseInt(adviserIdStr, 10, 64)
	if err != nil {
		c.JSON(fiber.Map{
			"code": 500,
			"message": err.Error(),
			"data": nil,
		})
	}

	res, err := cc.Model.QueryToDayNote(adviserId, date)
	if err != nil{
		c.JSON(fiber.Map{
			"code": 500,
			"message": err.Error(),
			"data": nil,
		})
	}

	if err := c.JSON(fiber.Map{
		"code": 200,
		"message": "成功",
		"data": res,
	}); err != nil {
		c.Status(500).Send(err)
		return
	}
}

func (cc *CalendarController)QueryWeekNote(c *fiber.Ctx){
	cc.Model = new(model.Note)

	adviserIdStr := c.FormValue("adviserId")
	adviserId, err := strconv.ParseInt(adviserIdStr, 10, 64)
	if err != nil {
		c.JSON(fiber.Map{
			"code": 500,
			"message": err.Error(),
			"data": nil,
		})
	}

	res, err := cc.Model.QueryWeekNote(adviserId)
	if err != nil{
		c.JSON(fiber.Map{
			"code": 500,
			"message": err.Error(),
			"data": nil,
		})
	}

	if err := c.JSON(fiber.Map{
		"code": 200,
		"message": "成功",
		"data": res,
	}); err != nil {
		c.Status(500).Send(err)
		return
	}
}

func (cc *CalendarController)QueryWeekNoteAsync(c *fiber.Ctx){
	cc.Model = new(model.Note)

	adviserIdStr := c.FormValue("adviserId")
	adviserId, err := strconv.ParseInt(adviserIdStr, 10, 64)
	if err != nil {
		c.JSON(fiber.Map{
			"code": 500,
			"message": err.Error(),
			"data": nil,
		})
	}

	res, err := cc.Model.QueryWeekNoteAsync(adviserId)
	if err != nil{
		c.JSON(fiber.Map{
			"code": 500,
			"message": err.Error(),
			"data": nil,
		})
	}

	if err := c.JSON(fiber.Map{
		"code": 200,
		"message": "成功",
		"data": res,
	}); err != nil {
		c.Status(500).Send(err)
		return
	}
}

func (cc *CalendarController)QueryMonthNote(c *fiber.Ctx){
	cc.Model = new(model.Note)

	adviserIdStr := c.FormValue("adviserId")
	yearMonth := c.FormValue("yearMonth")

	adviserId, err := strconv.ParseInt(adviserIdStr, 10, 64)
	if err != nil {
		c.JSON(fiber.Map{
			"code": 500,
			"message": err.Error(),
			"data": nil,
		})
	}

	res, err := cc.Model.QueryMonthNote(adviserId, yearMonth)
	if err != nil{
		c.JSON(fiber.Map{
			"code": 500,
			"message": err.Error(),
			"data": nil,
		})
	}

	if err := c.JSON(fiber.Map{
		"code": 200,
		"message": "成功",
		"data": res,
	}); err != nil {
		c.Status(500).Send(err)
		return
	}
}

func (cc *CalendarController)QueryMonthNoteAsync(c *fiber.Ctx){
	cc.Model = new(model.Note)

	adviserIdStr := c.FormValue("adviserId")
	yearMonth := c.FormValue("yearMonth")

	adviserId, err := strconv.ParseInt(adviserIdStr, 10, 64)
	if err != nil {
		c.JSON(fiber.Map{
			"code": 500,
			"message": err.Error(),
			"data": nil,
		})
	}

	res, err := cc.Model.QueryMonthNoteAsync(adviserId, yearMonth)
	if err != nil{
		c.JSON(fiber.Map{
			"code": 500,
			"message": err.Error(),
			"data": nil,
		})
	}

	if err := c.JSON(fiber.Map{
		"code": 200,
		"message": "成功",
		"data": res,
	}); err != nil {
		c.Status(500).Send(err)
		return
	}
}

func (cc *CalendarController)SendMessage(c *fiber.Ctx){
	cc.Message = new(model.Message)

	res, err := cc.Message.SendMessage()
	if err != nil{
		c.JSON(fiber.Map{
			"code": 500,
			"message": err.Error(),
			"data": nil,
		})
	}

	if err := c.JSON(fiber.Map{
		"code": res["code"],
		"message": res["message"],
	}); err != nil {
		c.Status(500).Send(err)
		return
	}
}

func (cc *CalendarController)SendMessageAsync(c *fiber.Ctx){
	cc.Message = new(model.Message)

	res, err := cc.Message.SendMessageAsync()
	if err != nil{
		c.JSON(fiber.Map{
			"code": 500,
			"message": err.Error(),
			"data": nil,
		})
	}

	if err := c.JSON(fiber.Map{
		"code": res["code"],
		"message": res["message"],
	}); err != nil {
		c.Status(500).Send(err)
		return
	}
}
